#pragma once

#include <iostream>
#include <pthread.h>

// 防止冲突
namespace LockModule{

    class Mutex
    {
    public:
        Mutex(const Mutex &) = delete;
        const Mutex &operator=(const Mutex &) = delete;

        Mutex()
        {
            int n = ::pthread_mutex_init(&_lock, nullptr);
            // if(n == 0)

        }
        ~Mutex()
        {
            int n = ::pthread_mutex_destroy(&_lock);
        }

        void lock()
        {
            int n = ::pthread_mutex_lock(&_lock);
        }

        void unlock()
        {
            int n = ::pthread_mutex_unlock(&_lock);
        }
        pthread_mutex_t* lockptr()
        {
            return &_lock;
        }

    private:
        pthread_mutex_t _lock;
    };
    //
    //  Resource Acquisition is Initialization
    // RAII 
    class LockGuard
    {
    public:
        LockGuard(Mutex & mtx)
            : _mtx(mtx)
        {
            _mtx.lock();
        }
        ~LockGuard()
        {
            _mtx.unlock();
        }
    private:
        Mutex &_mtx;
    };


};


